package com.api.shopapi.mapper;

import com.api.shopapi.entity.EHealthRecord;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
@Mapper
public interface EHealthRecordMapper {

    @Insert("insert into e_health_record" +
            "(" +
            "EHealthRecordCode," +
            "ElderlyCode," +
            "Name," +
            "ConfirmedTime," +
            "Remarks," +
            "IllnessStatus," +
            "BloodTraVol," +
            "ReasonInfusion," +
            "Status," +
            "Relation," +
            "Hospital" +
            ") " +
            "values" +
            "(" +
            "#{eHealthRecord.EHealthRecordCode}," +
            "#{eHealthRecord.ElderlyCode}," +
            "#{eHealthRecord.Name}," +
            "#{eHealthRecord.ConfirmedTime}," +
            "#{eHealthRecord.Remarks}," +
            "#{eHealthRecord.IllnessStatus}," +
            "#{eHealthRecord.BloodTraVol}," +
            "#{eHealthRecord.ReasonInfusion}," +
            "#{eHealthRecord.Status}," +
            "#{eHealthRecord.Relation}," +
            "#{eHealthRecord.Hospital}" +
            ")")
    int insert(@Param("eHealthRecord") EHealthRecord eHealthRecord);

    @Update("update e_health_record set" +
            " ElderlyCode=#{eHealthRecord.ElderlyCode}," +
            " Name=#{eHealthRecord.Name}," +
            " ConfirmedTime=#{eHealthRecord.ConfirmedTime}," +
            " Remarks=#{eHealthRecord.Remarks}," +
            " IllnessStatus=#{eHealthRecord.IllnessStatus}," +
            " BloodTraVol=#{eHealthRecord.BloodTraVol}," +
            " ReasonInfusion=#{eHealthRecord.ReasonInfusion}," +
            " Status=#{eHealthRecord.Status}," +
            " Relation=#{eHealthRecord.Relation}," +
            " Hospital=#{eHealthRecord.Hospital}" +
            " where EHealthRecordCode=#{eHealthRecord.EHealthRecordCode}")
    int update(@Param("eHealthRecord") EHealthRecord eHealthRecord);


    @Select("select * from e_health_record where eHealthRecordCode=#{eHealthRecordCode} limit 1")
    EHealthRecord getByEHealthRecordCode(@Param("eHealthRecordCode") String eHealthRecordCode);


    @Select("<script>" +
            "select * from e_health_record " +
            "where Status=#{status} and ElderlyCode=#{elderlyCode}" +
            "<if test ='name !=\"\" '>" +
            " and name like concat('%',#{name},'%')" +
            "</if>" +
            "<if test ='hospital !=\"\" '>" +
            " and hospital like concat('%',#{hospital},'%')" +
            "</if>" +
            "<if test ='bloodTraVol !=\"\" '>" +
            " and bloodTraVol=#{bloodTraVol}" +
            "</if>" +
            "<if test ='reasonInfusion !=\"\" '>" +
            " and reasonInfusion like concat('%',#{reasonInfusion},'%')" +
            "</if>" +
            "<if test ='relation !=null '>" +
            " and relation=#{relation}" +
            "</if>" +
            "<if test ='isTime !=\"\" '>" +
            " and (ConfirmedTime between #{startTime} and #{endTime})" +
            "</if>" +
            "</script>")
    List<EHealthRecord> getPageList(@Param("name") String name,
                                    @Param("hospital") String hospital,
                                    @Param("bloodTraVol") String bloodTraVol,
                                    @Param("reasonInfusion") String reasonInfusion,
                                    @Param("status") int status,
                                    @Param("isTime") String isTime,
                                    @Param("startTime") Date startTime,
                                    @Param("endTime") Date endTime,
                                    @Param("elderlyCode") String elderlyCode,
                                    @Param("relation") Integer relation
                                    );


    @Select("select * from e_health_record where ElderlyCode=#{elderlyCode}")
    List<EHealthRecord> getListByElderlyCode(@Param("elderlyCode") String elderlyCode);

}
